package com.ticket.sass.admin.dto.output.pricecheme;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ticket.sass.admin.dto.output.WeekTimeOutput;
import com.ticket.sass.admin.model.LabModel;
import com.ticket.sass.common.entity.tenant.FilmEntity;
import com.ticket.sass.common.entity.tenant.HallEntity;
import com.ticket.sass.common.entity.tenant.PriceSchemeEntity;
import com.ticket.sass.common.entity.tenant.PriceSchemeMemberPriceEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ywb
 * @date 2024-03-07 10:55
 */
@Data
@ApiModel("票价方案")
public class PriceSchemeListOutput {
    @ApiModelProperty(value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    
    @ApiModelProperty(value = "方案名称")
    private String title;
    
    @ApiModelProperty(value = "生效类型：0 不限 1 指定")
    private Short activateType;
    
    @ApiModelProperty(value = "有效日期开始")
    private LocalDate startDate;
    
    @ApiModelProperty(value = "有效日期结束")
    private LocalDate endDate;
    
    @ApiModelProperty(value = "可用时段：0 不限制，1 指定范围")
    private Short availablePeriod;
    
    @ApiModelProperty(value = "星期")
    private List<WeekTimeOutput> weekTimes;
    
    @ApiModelProperty(value = "适用发行价")
    private List<BigDecimal> issuePrices;
    
    @ApiModelProperty(value = "影厅")
    private List<LabModel> hallLabs;
    
    @ApiModelProperty(value = "影片")
    private List<LabModel> filmLabs;
    
    @ApiModelProperty(value = "会员价")
    private List<MemberPriceOutput> memberPrices;
    
    @ApiModelProperty(value = "售价")
    private BigDecimal salePrice;
    
    @ApiModelProperty(value = "是否启用")
    private Boolean enable;
    
    public PriceSchemeListOutput() {
    }
    
    public PriceSchemeListOutput(PriceSchemeEntity entity) {
        this.id = entity.getId();
        this.title = entity.getTitle();
        this.startDate = entity.getStartDate();
        this.endDate = entity.getEndDate();
        this.salePrice = entity.getSalePrice();
        this.enable = entity.getEnable();
        this.availablePeriod = entity.getAvailablePeriod() != null ? entity.getAvailablePeriod().getValue() : 0;
        this.activateType = entity.getActivateType() != null ? entity.getActivateType().getValue() : 0;
        
        setIssuePrices(entity.getIssuePrices());
    }
    
    
    public void setIssuePrices(String json) {
        if (!StringUtils.isEmpty(json)) {
            try {
                this.issuePrices = new ObjectMapper().readValue(json, new TypeReference<>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public void setHallLabs(List<HallEntity> hallLabs) {
        List<LabModel> out = new ArrayList<>();
        for (HallEntity r : hallLabs) {
            if (r.getId() != 0) {
                out.add(new LabModel(r.getId(), r.getName()));
            }
        }
        this.hallLabs = out;
    }
    
    public void setFilmLabs(List<FilmEntity> filmLabs) {
        List<LabModel> out = new ArrayList<>();
        for (FilmEntity r : filmLabs) {
            if (r.getId() != 0) {
                out.add(new LabModel(r.getId(), r.getName()));
            }
        }
        this.filmLabs = out;
    }
    
    public void setMemberPrices(List<PriceSchemeMemberPriceEntity> memberPrices) {
        List<MemberPriceOutput> out = new ArrayList<>();
        for (PriceSchemeMemberPriceEntity r : memberPrices) {
            if (r.getMemberLevelId() != 0) {
                out.add(new MemberPriceOutput(r.getMemberLevelId(), r.getMemberLevelName(), r.getType().getValue(), r.getValue(), r.getFirstEnable()));
            }
        }
        this.memberPrices = out;
    }
}
